package com.wf.ew.sell.service.impl;

import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.sell.biz.SellSonBiz;
import com.wf.ew.sell.model.SellSon;
import com.wf.ew.sell.service.SellSonService;
import com.wf.ew.system.model.User;
import com.wf.ew.sell.biz.SellBiz;
import com.wf.ew.sell.model.Sell;
import com.wf.ew.sell.service.SellService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;

/**
 *
 * 数据服务层接口实现类
 * @author : Doman
 * @date : 2019-12-09 10:19:20
 * @version ：1.0
 */
@Service
public class SellServiceImpl implements SellService {

	@Autowired
	private SellBiz sellBiz;
	@Autowired
	private SellSonBiz sellSonBiz;
	@Autowired
	private SellSonService sellSonService;

	@Override
	public String list(PageResult<T> page, Sell sell, SellSon sellSon, User loginUser) {
		return sellBiz.list(page,sell,sellSon, loginUser);
	}
	
	@Override
	public List<Sell> list(Sell sell, User loginUser) {
		return sellBiz.list(sell,loginUser);
	}

	@Override
	public Sell getById(Sell sell) {
		return sellBiz.getById(sell);
	}

	@Transactional
	@Override
	public JsonResult add(Sell sell, User loginUser) {
		KeyHolder key= sellBiz.add(sell, loginUser);
		int keyid = key.getKey().intValue();
		if (keyid>0) {
			return JsonResult.ok("添加成功").put("keyid",keyid);
		}
		return JsonResult.error("添加失败");
	}

	@Override
	public JsonResult update(Sell sell, User loginUser) {
		if (sellBiz.update(sell, loginUser)) {
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Override
	public JsonResult updateState(Sell sell, User loginUser) {
		if (sellBiz.updateState(sell, loginUser)) {
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Override
	public JsonResult updateTotal(Sell sell, User loginUser) {
		if (sellBiz.updateTotal(sell, loginUser)) {
			return JsonResult.ok("修改成功");
		}
		return JsonResult.error("修改失败");
	}

	@Override
	public JsonResult delete(Sell sell, User loginUser) {
		if (sellBiz.delete(sell, loginUser)) {
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}

	@Transactional
	@Override
	public JsonResult deleteSellAndSon(Sell sell, User loginUser) {
		SellSon sellSon = null;
		String[] ids = sell.getIds().split(", ");
		if (ids != null && ids.length > 0) {
			for (int j = 0; j < ids.length; j++) {
				sellSon = new SellSon();
				sellSon.setSellId(ids[j]);
				List<SellSon> list = sellSonBiz.list(sellSon, loginUser);
				if (list != null && list.size() > 0) {
					for (int i = 0; i < list.size(); i ++) {
						sellSon = list.get(i);
						sellSon.setIds(sellSon.getId());
						sellSonService.delete(sellSon, loginUser);
					}
				}
			}
		}
		if (sellBiz.delete(sell, loginUser)) {
			return JsonResult.ok("删除成功");
		}
		return JsonResult.error("删除失败");
	}
}
